<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="仓库编码:">
        <el-input v-model="queryParams.warehouserCode" size="small" placeholder="请输入仓库编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库名称:">
        <el-input v-model="queryParams.warehouserName" size="small" placeholder="请输入仓库名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库类型:">
        <el-select v-model="queryParams.warehouseType" placeholder="请选择仓库类型" clearable size="small">
          <el-option v-for="option in options.warehouseTypeOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人邮件:">
        <el-input v-model="queryParams.contactEmail" size="small" placeholder="请输入联系人邮件" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:warehouse:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:warehouse:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:warehouse:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="查看库存" align="center" prop="warehouserCode" >
        <template slot-scope="scope">
         <el-button type="text" @click="handleView1(scope.row)">{{ scope.row.warehouserCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" align="center" prop="warehouserName" >
        <template slot-scope="scope">
         <el-button type="text" @click="handleView(scope.row)">{{ scope.row.warehouserName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="仓库类型" align="center" prop="warehouseType">
        <template slot-scope="scope">
          <span>{{ warehouseTypeFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人姓名" align="center" prop="contactName" />
      <el-table-column label="联系人电话" align="center" prop="contactPhone" />
      <el-table-column label="联系人邮件" align="center" prop="contactEmail" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:warehouse:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['basic:warehouse:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="warehouseFormDialog.title" :visible.sync="warehouseFormDialog.visible" :close-on-click-modal="false" width="580px" append-to-body>
      <el-form ref="warehouseForm" :model="warehouseFormData" :rules="warehouseFormRules" label-width="100px">
        <el-form-item label="仓库编码" prop="warehouserCode">
          <el-input v-model="warehouseFormData.warehouserCode" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouserName">
          <el-input v-model="warehouseFormData.warehouserName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="仓库类型" prop="warehouseType">
          <el-select v-model="warehouseFormData.warehouseType" style="width: 440px" clearable placeholder="请选择仓库类型">
            <el-option v-for="option in options.warehouseTypeOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="仓库城市" prop="destination">
          <el-cascader
            style="width: 100%;"
            v-model="warehouseFormData.destination"
            :options="locationList"
            :props="{ expandTrigger: 'hover', label: 'locationName', value: 'id' }"
            filterable
          />
        </el-form-item>

        <el-form-item label="地址" prop="contactName">
          <el-input v-model="warehouseFormData.address" placeholder="请输入联系人姓名" />
        </el-form-item>

        <el-form-item label="联系人姓名" prop="contactName">
          <el-input v-model="warehouseFormData.contactName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactPhone">
          <el-input v-model="warehouseFormData.contactPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="联系人邮件" prop="contactEmail">
          <el-input v-model="warehouseFormData.contactEmail" placeholder="请输入联系人邮件" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmWarehouseFormDialog">确 定</el-button>
        <el-button @click="resetWarehouseFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import WarehouseApi from '@/api/basic/WarehouseApi'
import DictValueApi from '@/api/system/DictValueApi'
import LocationApi from '@/api/basic/LocationApi'
import TreeUtils from '@/utils/TreeUtils'

const defaultWarehouseFormData = {
  warehouserCode: null,
  warehouserName: null,
  warehouseFullName: null,
  warehouseType: null,
  warehouseDesc: null,
  orgType: null,
  orgId: null,
  contactName: null,
  contactPhone: null,
  contactEmail: null,
  province: null,
  city: null,
  district: null,
  destination: null,
  address: null
}
export default {
  name: 'WarehouseList',
  data() {
    return {
      options: {
        warehouseTypeOptions: this.$constant.genWarehouseTypeOptions, // 仓库类型字典
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        warehouserCode: null,
        warehouserName: null,
        warehouseType: null,
        contactEmail: null,
        isValid: this.$constant.Y
      },
      locationList: [],
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      warehouseFormDialog: {
        title: '',
        visible: false
      },
      warehouseFormData: Object.assign({}, defaultWarehouseFormData),
      warehouseFormRules: {
        warehouserCode: [{ required: true, message: '仓库编码不能为空', trigger: 'blur' }],
        warehouserName: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      DictValueApi.list({ dictCode: 'bms_warehouse_type' }).then(response => {
        if(response.data.length>0){
          this.options.warehouseTypeOptions = response.data
        }
      })

      LocationApi.list().then(response => {
        this.locationList = TreeUtils.list2Tree(response.data)
      })
    },
    handleAdd() {
      this.openWarehouseFormDialog()
    },
    handleView(row) {
      this.$router.push({ path: '/warehouse/view/' + row.id })
    },
    handleView1(row) {
      this.$router.push({ path: '/warehouse/areaView/' + row.id })
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return WarehouseApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openWarehouseFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      WarehouseApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return WarehouseApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openWarehouseFormDialog(id) {
      this.resetWarehouseFormDialog()
      this.warehouseFormDialog.visible = true
      if (id) {
        this.warehouseFormDialog.title = '仓库-修改'
        WarehouseApi.get(id).then(response => {
          this.warehouseFormData.destination = []
          this.warehouseFormData.destination.push(response.data.province)
          this.warehouseFormData.destination.push(response.data.city)
          this.warehouseFormData.destination.push(response.data.district)

          this.warehouseFormData = {
            ...this.warehouseFormData,
            ...response.data
          }
        })
      } else {
        this.warehouseFormDialog.title = '仓库-添加'
      }
    },
    confirmWarehouseFormDialog() {
      this.$refs['warehouseForm'].validate(valid => {
        console.log(valid)
        console.log(this.warehouseFormData)
        if(this.warehouseFormData.destination==null){
          this.$message.error('仓库城市不能为空')
        }
        this.splitDestination()
        if (valid) {
          if (this.warehouseFormData.id) {
            console.log(this.warehouseFormData)
            WarehouseApi.edit(this.warehouseFormData).then(response => {
              this.$message.success('修改成功')
              this.warehouseFormDialog.visible = false
              this.handleQuery()
            })
          } else {
            WarehouseApi.add(this.warehouseFormData).then(response => {
              this.$message.success('添加成功')
              this.warehouseFormDialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetWarehouseFormDialog() {
      this.warehouseFormDialog = {
        title: '',
        visible: false
      }
      this.warehouseFormData = Object.assign({}, defaultWarehouseFormData)
      this.resetForm('warehouseForm')
    },
    // 仓库类型字典
    warehouseTypeFormat(row) {
      return this.$common.getDictValueName(this.options.warehouseTypeOptions, row.warehouseType)
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    splitDestination(){
      this.warehouseFormData.province = this.warehouseFormData.destination[0]
      this.warehouseFormData.city = this.warehouseFormData.destination[1]
      this.warehouseFormData.district = this.warehouseFormData.destination[2]
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
